﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Optimization;
using System.Web.Security;
using System.Web.SessionState;

namespace CustomersSuppliersWebApplication
{
    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {
            // Code that runs on application startup
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterOpenAuth();

            CustomersSuppliersDataLayer.DataBaseConnections.CustomersSuppliers = new AdoHelper.DataBase(System.Configuration.ConfigurationManager.ConnectionStrings["CustomersSuppliers"].ConnectionString, "System.Data.SqlClient");
            CashVariables.InitializeCash();

            //images for rotator
            HttpContext.Current.Cache.Insert("Rotator", new string[] { Server.MapPath("~/Xml/"), Server.MapPath("~/Images/Rotator/")}, 
               new System.Web.Caching.CacheDependency(Server.MapPath("~/Images/Rotator/")), 
               DateTime.Now.AddMinutes(60),TimeSpan.Zero,
               System.Web.Caching.CacheItemPriority.Default,
               new System.Web.Caching.CacheItemRemovedCallback(CacheItemRemovedCallBack));
        }

        private void CacheItemRemovedCallBack(string key, object value, System.Web.Caching.CacheItemRemovedReason reason)
        {
            if (key == "Rotator" && reason== System.Web.Caching.CacheItemRemovedReason.DependencyChanged)
            {
                //images in WebFormHome
                string xmlPath = ((string[])value)[0];
                System.IO.File.WriteAllText(xmlPath + "Rotator.xml", "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n<Images>");
                string[] files = System.IO.Directory.GetFiles(((string[])value)[1]);
                for (int i = 0; i < files.Length; i++)
                {
                    if (files[i].ToLower().EndsWith("jpg") || files[i].ToLower().EndsWith("png") || files[i].ToLower().EndsWith("jpg"))
                    {
                        string imgeUrl = "~/Images/Rotator/" + System.IO.Path.GetFileName(files[i]);
                        System.IO.File.AppendAllText(xmlPath + @"Rotator.xml", string.Format("\r\n\t<Image>\r\n\t\t<ImageUrl>{0}</ImageUrl></Image>", imgeUrl));
                    }
                }
                System.IO.File.AppendAllText(xmlPath + "Rotator.xml", "\r\n</Images>");
            }
        }

        protected void Session_Start(object sender, EventArgs e)
        {
            try
            {
            }
            catch
            {
            }
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            if (HttpContext.Current.Cache["Rotator"]!=null)
            {
                //images for rotator
                HttpContext.Current.Cache.Insert("Rotator", new string[] { Server.MapPath("~/Xml/"), Server.MapPath("~/Images/Rotator/") },
                   new System.Web.Caching.CacheDependency(Server.MapPath("~/Images/Rotator/")),
                   DateTime.Now.AddMinutes(60), TimeSpan.Zero,
                   System.Web.Caching.CacheItemPriority.Default,
                   new System.Web.Caching.CacheItemRemovedCallback(CacheItemRemovedCallBack));
            }

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}